//
//  MusicRequire.m
//  SysMenu
//
//  Created by Joymaster on 10-8-20.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MusicRequire.h"
#import "FileTools.h"



@implementation MusicRequire

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	MusicRequire *layer = [MusicRequire node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}



-(id) init{
	self=[super init];
	if (self) {
		//置屏幕触摸有效
		self.isTouchEnabled = YES;
		
		//获取语言文件   1:English 2:简体中文 3:繁体中文
		int language=0 ;
		CReader readFile;
		if(readFile.Open("GameLanguage"))
		{
			readFile.ReadInt(&language);
			readFile.Close();
		}
		else {
			language=1;
		}

		
		
		//背景
		bg =[CCSprite spriteWithFile:@"back_Hiphop.png"];
		bg.anchorPoint=CGPointZero;
		[self addChild:bg z:0 tag:1];
		
		//背景黑框
		CCSprite *bgblack=[CCSprite spriteWithFile:@"Askmusic_Bbox.png"];
		bgblack.position=ccp(240,160);
		[self addChild:bgblack z:1 tag:2];
		
		//您是否想要听自己得音乐？
		switch (language) {
			case 2:
				askWord=[CCSprite spriteWithFile:@"Askmusic_Word_Cn1.png" rect:CGRectMake(0, 0, 166, 30)];
				break;
			case 3:
				askWord=[CCSprite spriteWithFile:@"Askmusic_Word_Cn2.png" rect:CGRectMake(0, 0, 166, 30)];
				break;
			default:
				askWord=[CCSprite spriteWithFile:@"Askmusic_Word_En.png" rect:CGRectMake(0, 0, 166, 30)];
				break;
		}
		askWord.position=ccp(240,174);
		[self addChild:askWord z:2 tag:3];
		
		//选项背景黑框
		bgYes=[CCSprite spriteWithFile:@"Askmusic_Sbox.png" rect:CGRectMake(0, 0, 94, 22)];
		bgYes.position=ccp(178,135);
		[self addChild:bgYes z:2 tag:4];	
		
		bgNo=[CCSprite spriteWithFile:@"Askmusic_Sbox.png" rect:CGRectMake(0, 0, 94, 22)];
		bgNo.position=ccp(300,135);
		[self addChild:bgNo z:2 tag:5];		
		
		//选项是和否
		switch (language) {
			case 2:
				ansYes=[CCSprite spriteWithFile:@"Askmusic_Word_Cn1.png" rect:CGRectMake(166, 0, 22, 14)];
				ansNo=[CCSprite spriteWithFile:@"Askmusic_Word_Cn1.png" rect:CGRectMake(189, 0, 22, 14)];
				break;
			case 3:
				ansYes=[CCSprite spriteWithFile:@"Askmusic_Word_Cn2.png" rect:CGRectMake(166, 0, 22, 14)];
				ansNo=[CCSprite spriteWithFile:@"Askmusic_Word_Cn2.png" rect:CGRectMake(189, 0, 22, 14)];
				break;
			default:
				ansYes=[CCSprite spriteWithFile:@"Askmusic_Word_En.png" rect:CGRectMake(166, 0, 22, 14)];
				ansNo=[CCSprite spriteWithFile:@"Askmusic_Word_En.png" rect:CGRectMake(189, 0, 22, 14)];
				break;
		}
		ansYes.position=ccp(180,134);
		[self addChild:ansYes z:3 tag:6];	
		
		ansNo.position=ccp(300,134);
		[self addChild:ansNo z:3 tag:7];	

	}
	return self;
}


//选项yes操作
-(void) chYes{
	[bgYes setTextureRect:CGRectMake(0, 23, 94, 22)];
	
	//[[CCDirector sharedDirector] replaceScene:[CCSlideInLTransition transitionWithDuration:0.5f scene:[MainMenu scene]]];
}

//no操作
-(void) chNo{
	[bgNo setTextureRect:CGRectMake(0, 23, 94, 22)];	
	
	//[[CCDirector sharedDirector] replaceScene:[CCSlideInRTransition transitionWithDuration:0.5f scene:[MainMenu scene]]];
}


//取屏幕相应区域调用函数，改变选项背景框
-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
}

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
	
	
	CGPoint location = [touch locationInView: [touch view]];
	CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
	//Yes
	CGRect yesRect = CGRectMake(bgYes.position.x-bgYes.contentSize.width/2, bgYes.position.y-bgYes.contentSize.height/2, bgYes.contentSize.width, bgYes.contentSize.height);
	if (CGRectContainsPoint(yesRect, convertedLocation)) {
		[self chYes];
	}
	
	//No
	CGRect noRect = CGRectMake(bgNo.position.x-bgNo.contentSize.width/2, bgNo.position.y-bgNo.contentSize.height/2, bgNo.contentSize.width, bgNo.contentSize.height);
	if (CGRectContainsPoint(noRect, convertedLocation)) {
		[self chNo];
	}	
	
	return YES;
}

- (void)ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
	//CGPoint location = [touch locationInView: [touch view]];
	//CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
}

- (void)ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{
	//CGPoint location = [touch locationInView: [touch view]];
	//CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
}	






@end
